﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace SystemDiagramNew
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdSystemDiagram : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			this.CommandData = cmdData;
			SystemDiagram systemDiagram = new SystemDiagram(cmdData);
			Result result;
			try
			{
				if (!systemDiagram.Run())
				{
					YJKMessageBox.Show(systemDiagram.ErrorMessage);
				}
				else
				{
					string message2 = systemDiagram.SystemInfo.SysName + "创建成功!";
					YJKMessageBox.Show(message2);
				}
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			finally
			{
				systemDiagram.UnlockRevit();
				systemDiagram.Finish();
			}
			return result;
		}

		public ExternalCommandData CommandData { get; set; }

		public string Message { get; set; }

		public ElementSet Elements { get; set; }

		public UIApplication UIApplication
		{
			get
			{
				return this.CommandData.Application;
			}
		}

		public UIDocument UIDocument
		{
			get
			{
				return this.UIApplication.ActiveUIDocument;
			}
		}

		public Document Document
		{
			get
			{
				return this.UIDocument.Document;
			}
		}
	}
}
